import java.util.concurrent.*;

public class ThreadPoolTest {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 60,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(1),
            new DefaultThreadFactory(), new DefaultRejectHandler());


    public static void main(String[] args) {
        // 打印出23934342
        // System.out.println(new Outer().hashCode());
        Future<?> future = executor.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("第一个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("第2个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("第3个任务");
            }
        });
    }

    static class DefaultThreadFactory implements ThreadFactory {

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("aa");
            t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    System.out.println("异常了 " + e.getMessage());
                }
            });
            return t;
        }
    }

    static class DefaultRejectHandler implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("拒绝了");
        }
    }
}

